package utils;

import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JTable;

/**
 * 
 * @author D'Er'Nasherbrezon aka dernasherbrezon %)
 *
 */
public class ColumnGroup extends SimpleColumn
{
	public ArrayList<SimpleColumn> childs = new ArrayList<SimpleColumn>();
	
	 protected String text;
	 protected int margin=0;
	 
	 public ColumnGroup()
	 {
	 }
	 
	 public ColumnGroup(String text)
	 {
		 super(0,text);
		 this.text = text;
	 }
	 
    public void add(SimpleColumn col)
    {
        if (col == null)
        {
        	return; 
        }
        if(childs.size()==0)
		{
			realModelIndex = col.realModelIndex;
		}
        childs.add(col);
        col.parent = this;
    }
    
    public void remove(SimpleColumn child)
    {
    	if(child==null)
    	{
    		return;
    	}
    	child.parent = null;
    	childs.remove(child);
    }
    
    /**
     * Get the ColumnGroup list containing the required table
     * column.
     * @param g vector to populate with the ColumnGroup/s
     * @param c TableColumn
     * @return Vector containing the ColumnGroup/s
     */
    @SuppressWarnings("unchecked")
	public ArrayList<SimpleColumn> getColumnGroups(SimpleColumn c, ArrayList<SimpleColumn> g)
    {
        g.add(this);
        if (childs.contains(c))
        {
        	return g;
        }
        
        for(int i=0;i<childs.size();i++)
        {
        	SimpleColumn curCol = childs.get(i);
        	if (curCol instanceof ColumnGroup)
        	{
        		ArrayList<SimpleColumn> groups = ((ColumnGroup)curCol).getColumnGroups(c,(ArrayList)g.clone());
	            if (groups != null)
	            {
	            	return groups;
	            }
        	}
        }
        return null;
    }
    
    public Object getHeaderValue()
    {
        return text;
    }
    
    @Override
    public void setHeaderValue(Object headerValue)
    {
    	text = headerValue.toString();
    }
    
    
    @Override
    public int getWidth()
    {
        int width  = 0;
        for(int i=0;i<childs.size();i++)
        {
        	SimpleColumn curCol = childs.get(i);
        	width += curCol.getWidth();
        }
        return width;
    }
    
    public Dimension getSize(JTable table)
    {
        Component comp = getHeaderRenderer().getTableCellRendererComponent(table, getHeaderValue(), false, false,-1, -1);
        int height = comp.getPreferredSize().height;
        int width  = 0;
        for(int i=0;i<childs.size();i++)
        {
        	SimpleColumn curCol = childs.get(i);
        	width += curCol.getSize(table).width;
        }
        return new Dimension(width, height);
    }
    
    public void setColumnMargin(int margin)
    {
        this.margin = margin;
        for(int i=0;i<childs.size();i++)
        {
        	SimpleColumn curCol = childs.get(i);
        	curCol.setColumnMargin(margin);
        }
    }
    
    
    public void setFreeze(boolean isFreeze)
    {
    	int retValue = 0;
    	for(int i=0;i<childs.size();i++)
    	{
    		if(childs.get(i).isFreeze())
    		{
    			retValue++;
    		}
    	}
    	if(isFreeze)
    	{
			this.isFreeze = true;
    		if(retValue==childs.size())
    		{
    			isVisible = true;
    		}
    		if(retValue==1)
    		{
    			isVisible = false;
    		}
    	}
    	else
    	{
			this.isFreeze = false;
    		if(retValue==childs.size()-1)
    		{
    			isVisible = false;
    		}
    		if(retValue==0)
    		{
    			isVisible = true;
    		}
    	}
    	
    	if(parent!=null)
    	{
    		parent.setFreeze(isFreeze);
    	}
    }
}
